const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    is_shop:2,
    shop_id:null,
    nav: [
      { title: '店铺管理', icon: 'edit', url: '/pages/mine/shop/takeout/edit', color_id: 5 },
      { title: '商品管理', icon: 'gouwudai', url: './goods/list', color_id: 1 },
      { title: '餐饮商品管理', icon: 'gouwudai', url: './goods/list2', color_id: 1 },
      { title: '店员管理', icon: 'qrcode', url: './clerk/clerk', color_id: 6 },
      { title: '订单管理', icon: 'log', url: './takeout/dingdan/list', color_id: 6 },
      // { title: '参与同城卡', icon: 'bbs', url: 'showABS', color_id: 5 },
      { title: '优惠券管理', icon: 'gif', url: './coupon/list2', color_id: 4 },
      { title: '我要提现', icon: 'doc', url: './takeout/tixian/tixian', color_id: 8 },
      { title: '积分管理', icon: 'drop-sort', url: './integral/integral', color_id: 3},
    ],
    add:[
      { title: '添加店铺', icon: 'gouwudai', url: './takeout/edit', color_id: 1 },
    ],
    storeInfo: []
  },
  backto: function (e) {
    wx.navigateBack({})
  },
  //店铺营业开关
  switch1Change: function (e) {
    var that = this;
    //营业
    if (e.detail.value){
      var is_rest = 2;
    }else{
      var is_rest = 1;
    }
    app.ihttpGet("https://www.58zltc.com/api.php/TakeOut/setRest/openid/" + app.globalData.openid +'/is_rest/'+is_rest, function (res) {
      that.setData({
       checked:(is_rest==1)?false:true
      })
    })
   
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    wx.showLoading({
      title: '加载中…',
    })
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    this.setData({
      token: extConfig.appid
    })
    var openid= app.globalData.openid;
    app.ihttpGet('https://www.58zltc.com/api.php/TakeOut/getCardInfo/openid/' + openid, function (resqq) {
      var navt = that.data.nav;
      if (resqq.data.status == 1) {
        for (var i = 0; i < navt.length; i++) {
          if (navt[i].title == '参与同城卡') {
            console.log(navt[i].title + i)
            navt[i].title = '管理同城卡'
          }
        }
      }
      that.setData({
        nav: navt
      })
    })

    wx.request({
      url: 'https://www.58zltc.com/api.php/TakeOut/findStoreInfo?token=' + that.data.token,
      method: 'POST',
      data: {
        openid:openid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (a) {
        that.setData({
          is_shop: a.data.status
        });
        if(a.data.status == 1){
          that.setData({
            shop_id: a.data.shop_id
          });
        }
        wx.hideLoading();
      }
    });
    app.ihttpGet("https://www.58zltc.com/api.php/TakeOut/getTakeoutStoreInfo/openid/" + app.globalData.openid, function (res) {
      var have_store = 0;
      if (res.data) {
        have_store = 1;
        if (res.data.is_rest == 0) {
          var checked = true;
        } else {
          var checked = false;
        }
        that.setData({
          storeInfo: res.data,
          have_store: have_store,
          checked: checked,
          wid: res.data.wid
        })
      }
      
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  jump: function (options) {
    var that = this;
    var url = options.currentTarget.dataset.url;
    var id = options.currentTarget.dataset.id;
    if (that.data.is_shop == 1) {
      url = url + "?shop_id=" + id + '&wid=' + that.data.wid + '&type=takeout';
      wx.navigateTo({
        url: url,
      })
    } else {
      wx.navigateTo({
        url: url,
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onLoad();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})